package com.digital.smartgrow.service.expert.random;

import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * Created by SunYuechao on 2022/7/16 下午8:04
 * 随机生成一个
 */

public class RandomExpertUtil {

    public   static  RandomExpert randomOne(List<RandomExpert> list, Random random){
        //按照权重排序
        Collections.sort(list,new Comparator<RandomExpert>() {
            @Override
            public int compare(RandomExpert o1, RandomExpert o2) {
                if(o1.getWeight()>o2.getWeight()){
                    return -1;
                }else if(o1.getWeight()>o2.getWeight()){
                    return 1;
                }
                return 0;
            }
        });
        int sum = 0;
        for(RandomExpert object:list){
            //System.out.println(object.getWeight());
            sum += object.getWeight();
        }
        int randomNumber = random.nextInt(sum);
        RandomExpert target = null;
        for(int i=0;i<list.size();i++){
            RandomExpert randomExpert = list.get(i);
            randomNumber -= randomExpert.getWeight();
            if(randomNumber <=0){
                target = randomExpert;
                break;
            }
        }
        for(RandomExpert object:list){

        }
        return target;
    }
}
